<template>
  <div class="profile-root">
    <el-card class="profile-card">
      <div class="profile-header">
        <el-upload
          class="avatar-uploader"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          action="https://jsonplaceholder.typicode.com/posts/"
        >
          <el-avatar
            :size="80"
            :src="user.avatar"
            style="cursor:pointer;"
          />
        </el-upload>
        <div class="profile-info">
          <div class="profile-username">
            {{ user.username }}
          </div>
          <div class="profile-email">
            {{ user.email }}
          </div>
        </div>
      </div>
      <el-divider />
      <el-form
        ref="profileFormRef"
        :model="user"
        label-width="80px"
        class="profile-form"
        :rules="rules"
      >
        <el-form-item
          label="用户名"
          prop="username"
        >
          <el-input v-model="user.username" />
        </el-form-item>
        <el-form-item
          label="邮箱"
          prop="email"
        >
          <el-input v-model="user.email" />
        </el-form-item>
        <el-form-item
          label="手机号"
          prop="phone"
        >
          <el-input v-model="user.phone" />
        </el-form-item>
      </el-form>
      <div class="profile-footer">
        <el-button @click="showPwdDialog = true">
          修改密码
        </el-button>
        <el-button
          type="primary"
          @click="saveProfile"
        >
          保存
        </el-button>
      </div>
    </el-card>
    <!-- 修改密码弹窗 -->
    <el-dialog
      v-model="showPwdDialog"
      title="修改密码"
      width="360px"
    >
      <el-form
        ref="pwdFormRef"
        :model="pwdForm"
        :rules="pwdRules"
      >
        <el-form-item
          label="原密码"
          prop="oldPwd"
        >
          <el-input
            v-model="pwdForm.oldPwd"
            type="password"
          />
        </el-form-item>
        <el-form-item
          label="新密码"
          prop="newPwd"
        >
          <el-input
            v-model="pwdForm.newPwd"
            type="password"
          />
        </el-form-item>
        <el-form-item
          label="确认密码"
          prop="confirmPwd"
        >
          <el-input
            v-model="pwdForm.confirmPwd"
            type="password"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showPwdDialog = false">
          取消
        </el-button>
        <el-button
          type="primary"
          @click="changePwd"
        >
          保存
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

interface UserProfile {
  username: string
  email: string
  phone: string
  avatar: string
}

const user = ref<UserProfile>({
  username: '学生A',
  email: 'student@example.com',
  phone: '13800000000',
  avatar: 'https://img1.imgtp.com/2023/07/21/1.jpg'
})

const profileFormRef = ref()
const rules = reactive({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ]
})

// 头像上传相关（模拟）
const handleAvatarSuccess = (response: any, file: any) => {
  user.value.avatar = file.url || file.thumbUrl || ''
  ElMessage.success('上传成功（模拟）')
}
const beforeAvatarUpload = (file: File) => {
  const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
  const isLt2M = file.size / 1024 / 1024 < 2
  if (!isJPG) ElMessage.error('只能上传 JPG/PNG 图片!')
  if (!isLt2M) ElMessage.error('图片大小不能超过 2MB!')
  return isJPG && isLt2M
}

const saveProfile = () => {
  profileFormRef.value.validate((valid: boolean) => {
    if (!valid) return
    ElMessage.success('保存成功！')
  })
}

// 修改密码弹窗相关
const showPwdDialog = ref(false)
const pwdForm = ref({ oldPwd: '', newPwd: '', confirmPwd: '' })
const pwdFormRef = ref()
const pwdRules = reactive({
  oldPwd: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
  newPwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '新密码至少6位', trigger: 'blur' }
  ],
  confirmPwd: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { validator: (rule: any, value: string, callback: any) => {
      if (value !== pwdForm.value.newPwd) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }, trigger: 'blur' }
  ]
})
const changePwd = () => {
  pwdFormRef.value.validate((valid: boolean) => {
    if (!valid) return
    showPwdDialog.value = false
    ElMessage.success('密码修改成功！')
    pwdForm.value = { oldPwd: '', newPwd: '', confirmPwd: '' }
  })
}
</script>

<style scoped>
.profile-root {
  max-width: 500px;
  margin: 0 auto;
  padding: 32px 0 40px 0;
}
.profile-card {
  border-radius: 14px;
  box-shadow: 0 2px 12px #e6e6e6;
  padding: 32px 24px 24px 24px;
}
.profile-header {
  display: flex;
  align-items: center;
  gap: 24px;
  margin-bottom: 12px;
}
.profile-info {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.profile-username {
  font-size: 1.3rem;
  font-weight: 600;
  color: #222;
}
.profile-email {
  color: #888;
  font-size: 1rem;
}
.profile-form {
  margin-top: 18px;
}
.profile-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 18px;
}
.avatar-uploader .el-avatar {
  border: 2px solid #eee;
  transition: box-shadow 0.2s;
}
.avatar-uploader .el-avatar:hover {
  box-shadow: 0 2px 8px #409eff44;
}
</style> 